package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.enums.CustomerLevelPrivilegeEnum;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.customer.CustomerLevelInfo;
import com.probox.common.entity.domain.customer.CustomerPrivilege;
import com.probox.common.entity.domain.customer.LevelPrivilege;
import com.probox.common.entity.mapper.customer.CustomerPrivilegeMapper;
import com.probox.common.entity.service.customer.ICustomerLevelInfoService;
import com.probox.common.entity.service.customer.ICustomerPrivilegeService;
import com.probox.common.entity.service.customer.ILevelPrivilegeService;
import com.probox.customer.api.domain.LevelAndPrivilege;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户权益Service业务层处理
 *
 * @author zsy
 * @date 2021-05-27
 */
@Service
@Transactional
public class CustomerPrivilegeServiceImpl extends ServiceImpl<CustomerPrivilegeMapper, CustomerPrivilege> implements ICustomerPrivilegeService {
    @Autowired
    private CustomerPrivilegeMapper customerPrivilegeMapper;

    @Autowired
    private ILevelPrivilegeService levelPrivilegeService;

    @Autowired
    private ICustomerLevelInfoService customerLevelInfoService;

    /**
     * 查询用户的打折
     *
     * @param customerUid
     * @return
     */
    @Override
    public CustomerPrivilege getByCustomerUid(Long customerUid) {
        CustomerLevelInfo level = customerLevelInfoService.getLevel(customerUid);
        if (ObjectUtil.isEmpty(level)) {
            return null;
        }
        //查询折扣的权益
        LevelPrivilege levelPrivilege = levelPrivilegeService.selectByName(CustomerLevelPrivilegeEnum.discount.getKey());
        if (ObjectUtil.isEmpty(levelPrivilege)) return null;
        QueryWrapper<CustomerPrivilege> queryWrapper = Wrappers.query();
        queryWrapper.eq("data_status", DataStatus.valid.getKey());
        queryWrapper.eq("LEVEL_UID", level.getLevelUid());
        queryWrapper.eq("PRIVILEGE_UID", levelPrivilege.getPrivilegeUid());
        queryWrapper.last(" order by CUSTOMER_PRIVILEGE_UID desc limit 1 ");
        CustomerPrivilege privilege = customerPrivilegeMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(privilege)) return null;
        privilege.setCustomeRgrade(level.getCustomeRgrade());
        return privilege;
    }


    @Override
    public CustomerPrivilege queryOne(CustomerPrivilege customerPrivilege) {
        QueryWrapper<CustomerPrivilege> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getCustomerPrivilegeUid())) && customerPrivilege.getCustomerPrivilegeUid() != null) {
            queryWrapper.eq("CUSTOMER_PRIVILEGE_UID", customerPrivilege.getCustomerPrivilegeUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getPlatformId())) && customerPrivilege.getPlatformId() != null) {
            queryWrapper.eq("PLATFORM_ID", customerPrivilege.getPlatformId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getPrivilegeUid())) && customerPrivilege.getPrivilegeUid() != null) {
            queryWrapper.eq("PRIVILEGE_UID", customerPrivilege.getPrivilegeUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getLevelUid())) && customerPrivilege.getLevelUid() != null) {
            queryWrapper.eq("LEVEL_UID", customerPrivilege.getLevelUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getIsUpgrade())) && customerPrivilege.getIsUpgrade() != null) {
            queryWrapper.eq("IS_UPGRADE", customerPrivilege.getIsUpgrade());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getPrivilegeValue())) && customerPrivilege.getPrivilegeValue() != null) {
            queryWrapper.eq("PRIVILEGE_VALUE", customerPrivilege.getPrivilegeValue());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getPrivilegeStatus())) && customerPrivilege.getPrivilegeStatus() != null) {
            queryWrapper.eq("PRIVILEGE_STATUS", customerPrivilege.getPrivilegeStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getCreateUser())) && customerPrivilege.getCreateUser() != null) {
            queryWrapper.eq("CREATE_USER", customerPrivilege.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getUpdateUser())) && customerPrivilege.getUpdateUser() != null) {
            queryWrapper.eq("UPDATE_USER", customerPrivilege.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getDataStatus())) && customerPrivilege.getDataStatus() != null) {
            queryWrapper.eq("DATA_STATUS", customerPrivilege.getDataStatus());
        }
        return customerPrivilegeMapper.selectOne(queryWrapper);
    }


    /**
     * 查询客户权益
     *
     * @param customerPrivilegeUid 客户权益ID
     * @return 客户权益
     */
    @Override
    public CustomerPrivilege selectCustomerPrivilegeById(Long customerPrivilegeUid) {
        return this.getById(customerPrivilegeUid);
    }

    /**
     * 查询客户权益列表
     *
     * @param customerPrivilege 客户权益
     * @return 客户权益
     */
    @Override
    public List<CustomerPrivilege> queryList(CustomerPrivilege customerPrivilege) {
        QueryWrapper<CustomerPrivilege> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getCustomerPrivilegeUid())) && customerPrivilege.getCustomerPrivilegeUid() != null) {
            queryWrapper.eq("CUSTOMER_PRIVILEGE_UID", customerPrivilege.getCustomerPrivilegeUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getPlatformId())) && customerPrivilege.getPlatformId() != null) {
            queryWrapper.eq("PLATFORM_ID", customerPrivilege.getPlatformId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getPrivilegeUid())) && customerPrivilege.getPrivilegeUid() != null) {
            queryWrapper.eq("PRIVILEGE_UID", customerPrivilege.getPrivilegeUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getLevelUid())) && customerPrivilege.getLevelUid() != null) {
            queryWrapper.eq("LEVEL_UID", customerPrivilege.getLevelUid());
        }
//        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getIsUpgrade())) && customerPrivilege.getIsUpgrade() != null) {
//            queryWrapper.eq("IS_UPGRADE", customerPrivilege.getIsUpgrade());
//        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getPrivilegeValue())) && customerPrivilege.getPrivilegeValue() != null) {
            queryWrapper.eq("PRIVILEGE_VALUE", customerPrivilege.getPrivilegeValue());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getPrivilegeStatus())) && customerPrivilege.getPrivilegeStatus() != null) {
            queryWrapper.eq("PRIVILEGE_STATUS", customerPrivilege.getPrivilegeStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getCreateUser())) && customerPrivilege.getCreateUser() != null) {
            queryWrapper.eq("CREATE_USER", customerPrivilege.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getUpdateUser())) && customerPrivilege.getUpdateUser() != null) {
            queryWrapper.eq("UPDATE_USER", customerPrivilege.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerPrivilege.getDataStatus())) && customerPrivilege.getDataStatus() != null) {
            queryWrapper.eq("DATA_STATUS", customerPrivilege.getDataStatus());
        }
        return customerPrivilegeMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户权益
     *
     * @param customerPrivilege 客户权益
     * @return 结果
     */
    @Override
    public boolean insertCustomerPrivilege(CustomerPrivilege customerPrivilege) {

        return this.save(customerPrivilege);

    }

    /**
     * 修改客户权益
     *
     * @param customerPrivilege 客户权益
     * @return 结果
     */
    @Override
    public boolean updateCustomerPrivilege(CustomerPrivilege customerPrivilege) {

        return this.updateById(customerPrivilege);
    }

    /**
     * 批量删除客户权益
     *
     * @param customerPrivilegeUids 需要删除的客户权益ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerPrivilegeByIds(Long[] customerPrivilegeUids) {
        UpdateWrapper<CustomerPrivilege> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("CUSTOMER_PRIVILEGE_UID", customerPrivilegeUids);
        updateWrapper.ne("status", DataStatus.delete.getKey());
        updateWrapper.set("status", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_USER", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }

    @Override
    public List<CustomerPrivilege> getLevelAndPrivilegeRelation() {
        return customerPrivilegeMapper.getLevelAndPrivilegeRelation();
    }

    @Override
    public List<LevelAndPrivilege> getLevelAndPrivilege(Long levelId) {
        return customerPrivilegeMapper.getLevelAndPrivilege(levelId);
    }


}
